Skip to contentMethod: MappingFinder(Finder, Function)
1: /*
2: * *************************************************************************************************************************************************************
3: *
4: * TheseFoolishThings: Miscellaneous utilities
5: * http://tidalwave.it/projects/thesefoolishthings
6: *
7: * Copyright (C) 2009 - 2025 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *************************************************************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
12: * You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
17: * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
18: *
19: * *************************************************************************************************************************************************************
20: *
21: * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
22: * git clone https://github.com/tidalwave-it/thesefoolishthings-src
23: *
24: * *************************************************************************************************************************************************************
25: */
26: package it.tidalwave.util.impl.finder;
27:
28: // import javax.annotation.concurrent.Immutable;
29: import jakarta.annotation.Nonnull;
30: import java.util.List;
31: import java.util.function.Function;
32: import it.tidalwave.util.Finder;
33: import it.tidalwave.util.spi.SimpleFinderSupport;
34: import static java.util.stream.Collectors.*;
35:
36: /***************************************************************************************************************************************************************
37: *
38: * A {@link it.tidalwave.util.Finder} which retrieves results from another instance applying a mapper {@link Function}. Don't use directly; use
39: * {@link it.tidalwave.util.Finder#mapping(Finder, Function)} instead.
40: *
41: * @param <T> the mapped type
42: * @param <U> the type of the delegate Finder
43: * @author Fabrizio Giudici
44: *
45: **************************************************************************************************************************************************************/
46: /* @Immutable */
47: public final class MappingFinder<T, U> extends SimpleFinderSupport<T>
48: {
49: private static final long serialVersionUID = -6359683808082070089L;
50:
51: @Nonnull
52: private final Finder<? extends U> delegate;
53:
54: @Nonnull
55: private final Function<? super U, ? extends T> mapper;
56:
57: /***********************************************************************************************************************************************************
58: * Creates a new instance given a delegate and a mapper function.
59: * @param delegate the delegate
60: * @param mapper the mapper function
61: **********************************************************************************************************************************************************/
62: public MappingFinder (@Nonnull final Finder<? extends U> delegate, @Nonnull final Function<? super U, ? extends T> mapper)
63: {
64: this.delegate = delegate;
65: this.mapper = mapper;
66: }
67:
68: /***********************************************************************************************************************************************************
69: * The special Finder copy constructor.
70: * @param other the finder to copy
71: * @param override the overriding object
72: **********************************************************************************************************************************************************/
73: @SuppressWarnings("unchecked")
74: public MappingFinder (@Nonnull final MappingFinder<T, ?> other, @Nonnull final Object override)
75: {
76: super(other, override);
77: final var source = getSource(MappingFinder.class, other, override);
78: this.delegate = source.delegate;
79: this.mapper = source.mapper;
80: }
81:
82: /***********************************************************************************************************************************************************
83: * {@inheritDoc}
84: **********************************************************************************************************************************************************/
85: @Override @Nonnull
86: protected List<T> computeResults()
87: {
88: return delegate.results().stream().map(mapper).collect(toList());
89: }
90: }